package com.atguigu.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.auth.feign.MemberFeignService;
import com.atguigu.common.vo.MemberResponseVo;
import com.atguigu.gulimall.auth.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @Author: Zjn ; Bring it on !!!
 * @Date: 2021/11/17 0017 20:21
 * Note:
 */
@Slf4j
@Controller
public class OAuth2Controller {

    @Autowired
    MemberFeignService memberFeignService;

    @GetMapping("/oauth2.0/gitee/success")
    public String gitee(@RequestParam("code") String code, HttpSession session) throws Exception {

        //1.根据code换取accessToken
        //https://gitee.com/oauth/token?
        // grant_type=authorization_code&
        // code={code}&client_id={client_id}&
        // redirect_uri={redirect_uri}&
        // client_secret={client_secret}
        HashMap<String, String> map = new HashMap<>();
        map.put("grant_type","authorization_code");
        map.put("code",code);
        map.put("client_id","e46212282755063410f7d1cb246f6bcdd4ac46afc1e796258f9207e1199571b5");
        map.put("redirect_uri","http://auth.gulimall.com/oauth2.0/gitee/success");
        map.put("client_secret","934269cdded83ff4d6fad81f39121a56fbee205936bae1ce7c05605eb8ae8a85");
        //1.根据code换取accessToken
        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token",
                "post", new HashMap<>(), map, new HashMap<>());
        //2.处理
        if (response.getStatusLine().getStatusCode()==200) {
            //获取到了accessToken
            //EntityUtils工具类可以将json数据转成字符串
            String json = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);

            //1.当前用户如果第一次登录该网站，则为其自动注册会员信息账号，并且关联当前登录的社交方式
            R oauthLogin = memberFeignService.oauthLogin(socialUser);
            if (oauthLogin.getCode()==0) {
                MemberResponseVo data = oauthLogin.getData("data", new TypeReference<MemberResponseVo>() {
                });
                //session 站内共享
                log.info("登录成功:用户{}",data.toString());
                session.setAttribute(AuthServerConstant.LOGIN_USER,data);
                //2.登录成功就跳回首页
                return "redirect:http://gulimall.com";
            }else{
                return "redirect:http://auth.gulimall.com/login.html";
            }
        }else{
            return "redirect:http://auth.gulimall.com/login.html";
        }
    }
}
